import useMultipartOssStore from "@/store/modules/multipartOssStore";
const multipartOssStore = useMultipartOssStore();
/**
 * 获取后缀
 */
const getSuffix = (fileName) => {
    return fileName.slice(fileName.lastIndexOf(".") + 1);
};
/**
 * 获取名称（不含后缀 
 */
const getFileName = (fileName) => {
    return fileName.slice(0, fileName.lastIndexOf("."));
}
/**
 * 随机生成ID
 * @return {string}
 */
export const generateId = () => {
    const str = [];
    const hexDigits = "0123456789abcdef";
    for (let i = 0; i < 36; i++) {
        str[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    str[14] = "4";
    str[19] = hexDigits.substr((str[19] & 0x3) | 0x8, 1);
    return str.join("");
};
export const isLegitimate=(options,types=[],size)=>{
    const { file } = options;
    if (!types.includes(file.type)) {
        return {msg:'图片格式错误'};
    } else if (file.size / 1024 > size) {
        return {msg:'图片大小超过限制'};
    }
    return true;
}
/**
 * 上传
 */
const upload = (options) => {
    const { file } = options;
    const fileName = `${getFileName(file.name)}-${generateId()}.${getSuffix(file.name)}`;
    return new Promise((resolve) => {
        multipartOssStore.submit(`finance/${fileName}`, file).then(async (res) => {
            await multipartOssStore.setACL(`finance/${fileName}`, "public-read");
            const fileUrl = await multipartOssStore.signatureUrl(`finance/${fileName}`);
            const url = fileUrl.split('?')[0];
            resolve({ url, name: fileName, uid: file.uid });
        });
    });
}
export default upload